// 输入：head = [1, 2, 3, 4, 5], n = 2
// 输出：[1, 2, 3, 5]

head = [1, 2, 3, 4, 5], n = 2

// var removeNthFromEnd = function (head, n) {
//     const dummy = new ListNode()
//     dummy.next = head
//     let cur = dummy
//     let temp = new ListNode()
//     temp = dummy
//     while (n >= 0) {
//         temp = temp.next
//         n--
//     }
//     while (temp) {
//         cur = cur.next
//         temp = temp.next
//     }
//     cur.next = cur.next.next
//     return dummy.next
// };

var removeNthFromEnd = function (head, n) {
    let dummy = new ListNode();
    dummy.next = head;
    let cur = dummy;
    let fast = dummy, slow = dummy;
    while (n > 0) {
        fast = fast.next;
        n--;
    }
    while (fast.next) {
        fast = fast.next;
        slow = slow.next;
    }
    slow.next = slow.next.next;
    return cur.next;
}